<?php

class Flang {
	private $mark = null;
	private $size = 32;
	
	public function __construct($mark = null, $size = 32) {
		$this->mark($mark);
		$this->size($size);
	}
	
	public function mark($mark = null) {
		if (is_null($mark)) {
			return $this->mark;
		}
		$mark = strtoupper($mark);
        if (preg_match('/^[A-F0-9]{64}$/', $mark)) {
            $this->mark = $mark;
        }
        return $this;
	}
	
	public function size($size = null) {
		if (is_null($size)) {
			return $this->size;
		}
		if ($size > 0 && $size <= 128 && ($size % 8) == 0) {
            $this->size = $size;
       	}
       	return $this;		
	}
	
	public function to_html() {
		$size = $this->size() / 8;
		$html = '<table cellpadding="0" cellspacing="0" border="0" class="flang">';
		foreach ($this->generate() as $key => $value) {
			$html .= '<tr>';
			foreach ($value as $k => $color) {
				$r = $color[0] ? $color[0] : 0; $g = $color[1] ? $color[1] : 0; $b = $color[2] ? $color[2] : 0;
				$html .= '<td style="width: ' . $size . 'px; height: ' . $size . 'px; background: rgb(' . $r . ', ' . $g . ', ' . $b . ')"></td>';
			}
			$html .= '</tr>';
		}
		$html .= '</table>';
		return $html;
	}
	
	public function to_image() {
        $size = $this->size() / 8;
        $image = imagecreate($this->size(), $this->size());
        imagecolorallocatealpha($image, 0, 0, 0, 127);
        foreach ($this->generate() as $key => $value) {
        	foreach ($value as $k => $color) {
				$x = $k * $size; $y = $key * $size;
				$r = $color[0] ? $color[0] : 0; $g = $color[1] ? $color[1] : 0; $b = $color[2] ? $color[2] : 0;
				imagefilledrectangle($image, $x, $y, $x + $size, $y + $size, imagecolorallocate($image, $r, $g, $b));		
        	}
        }
        header('Content-type: image/png', true);
        imagepng($image, false, 9);
        imagedestroy($image);
	}

	private function generate() {
		$h = 0;
		$pixels = array();
		for ($x = 0; $x < 8; $x++) {
			for ($y = 0; $y < 8; $y++) {
				$char = substr($this->mark(), $h++, 1);
				$color = $this->color($char);
				$pixels[$x][$y] = $color;
			}
		}
		return $pixels;
	}	
	
	private function color($str) {
        switch (strtolower($str)) {
            case 1: return array(0 ,0, 0);  break;
        	case 2: return array(128, 128, 128); break;
        	case 3: return array(255, 255, 255); break;
        	case 4: return array(254, 0, 0); break;
        	case 5: return array(255, 127, 0); break;
        	case 6: return array(255, 255, 0); break;
        	case 7: return array(128, 255, 0); break;
        	case 8: return array(0, 255, 1); break;
        	case 9: return array(0, 254, 129); break;
        	case 'a': return array(0, 255, 255); break;
        	case 'b': return array(0, 128, 255); break;
        	case 'c': return array(0, 0, 254); break;
        	case 'd': return array(127, 0, 255); break;
        	case 'e': return array(255, 0, 254); break;
        	case 'f': return array(255, 0, 128); break;
			default: return false; break;
		}
    }
}

?>